/* 

  VOTable parser


  Copyright © 2010 - 2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


  Reference:

  http://www.ivoa.net/Documents/VOTable/20091130/REC-VOTable-1.2.html

*/

#include "votable.h"
#include <wx/wx.h>
#include <wx/app.h>
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <wx/tokenzr.h>
#include <list>

using namespace std;

class VOTab: public wxAppConsole
{
public:

  bool OnInit();
  int OnRun();

private:

  int exitcode;
};

IMPLEMENT_APP_CONSOLE(VOTab)

// consider a class - the same as in cone.cpp
wxString GetString(const wxString& line)
{
  list<wxString> items;

  wxStringTokenizer tb = wxStringTokenizer(line,"'",wxTOKEN_STRTOK);
  while ( tb.HasMoreTokens() ) {
    wxString x = tb.GetNextToken();
    items.push_back(x);
  }

  return items.back();
}

double GetDouble(const wxString& line) 
{
  list<wxString> items;

  wxStringTokenizer tb = wxStringTokenizer(line,"=");
  while ( tb.HasMoreTokens() ) {
    wxString x = tb.GetNextToken();
    items.push_back(x);
  }

  wxString a = items.back();
  double x;
  
  if( a.ToDouble(&x) )
    return x;

  return 0.0;
}



bool VOTab::OnInit()
{
  wxString output, type, file, sort;
  wxString pt, mk;
  double pa, pd, ps, ml;

  wxFFileInputStream pstream(stdin);
  wxTextInputStream input(pstream); 

  while(pstream.IsOk() && ! pstream.Eof()) {

    wxString line = input.ReadLine();

    if( line.StartsWith("TYPE") )
      type = GetString(line);

    if( line.StartsWith("SORT") )
      sort = GetString(line);

    if( line.StartsWith("FILE") )
      file = GetString(line);

    if( line.StartsWith("PROJ TYPE") )
      pt = GetString(line);

    if( line.StartsWith("PROJ ALPHA") )
      pa = GetDouble(line);

    if( line.StartsWith("PROJ DELTA") )
      pd = GetDouble(line);

    if( line.StartsWith("PROJ SCALE") )
      ps = GetDouble(line);

    if( line.StartsWith("MAG LIMIT") )
      ml = GetDouble(line);

    if( line.StartsWith("MAG KEY") )
      mk = GetString(line);

    if( line.StartsWith("OUTPUT") )
      output = GetString(line);

    wxLogDebug(line);
  }


  wxFFileInputStream istream(file);
  VOTable vt(istream);

  if( ! sort.IsEmpty() )
    vt.Sort(sort);

  vt.SetProjection(pt);
  vt.SetProjectionCenter(pa,pd);
  vt.SetScale(ps);
  vt.SetMaglim(ml);
  vt.SetMagkey(mk);
  
  exitcode = vt.Save(output,type) ? 0 : 1;

  return true;
}

int VOTab::OnRun()
{
  wxLogDebug("OnRun: %d",exitcode);
  return exitcode;
}
